from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from openpyxl import Workbook


class Rank:
    def __init__(self):
        self.url = 'https://www.shanghairanking.cn/rankings/bcur/2023'
        self.headers = {
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) '
                          'Chrome/80.0.3987.132 Safari/537.36'
        }

    def crawl(self):
        # 设置Chrome选项，以便在后台运行（无界面）
        chrome_options = Options()
        chrome_options.add_argument("--headless")  # 无界面模式

        # 指定WebDriver的路径
        service = Service('D:\\data\\chromedriver.exe')

        # 创建WebDriver实例
        driver = webdriver.Chrome(service=service, options=chrome_options)

        # 打开目标网页
        driver.get(self.url)

        # 创建一个新的工作簿
        wb = Workbook()

        # 获取活动工作表（默认创建的第一个工作表）
        ws = wb.active
        ws.title = "数据表"

        # 写入标题行
        ws["A1"] = "排名"
        ws["B1"] = "大学名称"
        ws["C1"] = "省份"
        ws["D1"] = "类型"
        ws["E1"] = "总分"

        while True:
            try:
                # 等待分页按钮加载完成，这里使用隐式等待
                # 等待最多10秒
                driver.implicitly_wait(10)

                # 进行页面数据分析，并记录到excel中
                table = driver.find_element(By.XPATH, '//table[@class="rk-table"]')
                # 获取表格中的数据
                for row in table.find_elements(By.TAG_NAME, 'tr')[1:]:
                    tds = row.find_elements(By.TAG_NAME, 'td')
                    rank = tds[0].text.strip()
                    school_name = tds[1].text.strip()
                    province_city = tds[2].text.strip()
                    type = tds[3].text.strip()
                    total_score = tds[4].text.strip()

                    # 写入数据行
                    ws.append([rank, school_name, province_city, type, total_score])

                    # 打印获取到的数据
                    print(f'{rank} {school_name} {province_city} {type} {total_score}')

                next_button = driver.find_element(By.XPATH, '//li[@title="下一页"]')
                if(next_button.get_attribute('class').__contains__('ant-pagination-disabled')):
                    break;
                # 点击
                next_button.click()
            except Exception as ex:
                # 如果找不到下一页按钮或发生其他异常，则退出循环
                print("没有下一页或发生错误：", ex)
                break

        driver.quit()

        # 数据写入到excel文件
        wb.save('data.xlsx')

if __name__ == '__main__':
    rank = Rank()
    rank.crawl()